s1 = "abcevaefegsgdghfdefavbdr"

"""
统计各字符出现次数
返回一个字典，字符：次数
"""

def counter(src:str,sort = False,reversed = False,n = 0):
    """
    src:        待统计字符串
    sort:       是否对输出排序，默认不排序
    reversed：  升序or降序，默认升序
    n:          输出当前排序的前n位，默认不输出
    """
    count = {}
    for i in src:
        count[i] = count.get(i,0) + 1
    # [print(k,v,sep = '\t') for k,v in sorted(count.items(),key = lambda x:x[1],reverse = reversed)if sort ]  #另一种打印
    return [(k, v) for k, v in sorted(count.items(), key=lambda x: x[1], reverse=reversed)][:n] if sort else count
print(counter(s1,sort = True,reversed = True,n = -1))

def counter1(src:str,sort = False,reversed = False,n = 0):
    d = {}
    for i in